#include "precompiled.h"
#include "Gun.h"
#include "Camera_Alt.h"
#include "RenderManager.h"
#include "Common.h"
#include "Core.h"

const glm::vec3 BIASANGLES(30,30,0);

Gun::Gun(void)
{
}


Gun::~Gun(void)
{
}

void Gun::Initialize( float barrel_length ){
	butt = glm::vec4(0);
	head = butt;
	
	transform._position = glm::vec4(0);
	transform._translation = glm::vec3(0);
	transform._rotation = glm::vec3(0.0000001);
	transform._scale = glm::vec3(1);
	transform._model = glm::mat4(1);

	gun_forward = glm::vec3(FORWARD);

	model_key = std::string("gun_laser_blaster");

	rticket.bundle.modelKey = model_key;
	rticket.bundle.materialKey = "";
	rticket.bundle.transform = &transform;
}

// Rotate and Translate the gun into view.
void Gun::TransformToView( glm::mat4 const &view, glm::vec4 &player_position,
glm::vec3 &mouse_aim, glm::vec3 const &viewDir, glm::vec3 const &cam_hpr){

	// Transform our blaster.
	TransformToView_Blaster(view,player_position,mouse_aim,viewDir, cam_hpr);
}

// Rotate and Translate the gun into view.
void Gun::TransformToView_Blaster( glm::mat4 const &view, glm::vec4 &player_position,
	glm::vec3 &mouse_aim, glm::vec3 const &viewDir, glm::vec3 const &cam_hpr){


		// Translate our gun to the player's front.
		float dist = 2.0f;
		glm::vec3 gshift(0.0f, 0.0f, 0.0 );
		transform._position = glm::vec4(
			viewDir.x*dist+gshift.x, 
			viewDir.y*dist+gshift.y, 
			viewDir.z*dist+gshift.z, 0);
		transform._model = glm::translate( glm::mat4(1),
			glm::vec3( transform._position.x, transform._position.y, transform._position.z ) );
		butt = transform._position;
		head = butt + glm::vec4(0,0,1000,0);


		glm::mat4 r_aim( 1 );

		// Rotate by the mouse aim.
		/*float rrateY = 5.0f;
		float rrateX = 20.0f;
		r_aim = glm::rotate( r_aim, mouse_aim.y * rrateY, RIGHT);
		r_aim = glm::rotate( r_aim, mouse_aim.x * rrateX, UP);*/


		// Rotate to player.
		r_aim *= view;
		r_aim = glm::inverse(r_aim);
		transform._model *= r_aim;
		butt = r_aim * butt;
		head = r_aim * head;
		//butt *= -1.0f;
		//head *= -1.0f;

		// Adjust our forward.
		glm::vec4 tmp_f( head - butt );
		gun_forward = glm::vec3(tmp_f.x, tmp_f.y, tmp_f.z);

		// Translate the ass to the origin.
		transform._model = glm::translate( transform._model,
			glm::vec3( 1,-1.5f,-2 ) );
		transform._position += glm::vec4( 1.0f,-1.5f,-2.0f,0.0f );
		butt += glm::vec4( 1.0f,-1.5f,-2.0f, 0.0f );

		// Scale.
		transform._model = glm::scale( transform._model, glm::vec3(0.6f) );
		butt = butt * glm::vec4( 0.6f, 0.6f, 0.6f, 0.6f);
}

void Gun::Submit_Ticket( Camera_Alt* cam ){
	rticket.bundle.transform = &transform;
	rticket.distance = cam->getDistanceFromCamera( 
		glm::vec3(  transform._position.x,
		transform._position.y,
		transform._position.z ));
	rticket.shadargs=0;

	RendererManager::accept_renderTicket( rticket );
}

glm::vec3 const Gun::get_AimFrom() const{
	return glm::vec3( transform._position.x, transform._position.y, transform._position.z );
}
glm::vec3 const Gun::get_AimTo() const{
	return gun_forward;
}

glm::vec3 const Gun::get_butt() const{
	return glm::vec3( butt.x, butt.y, butt.z );
}
glm::vec3 const Gun::get_head() const{
	//return gun_forward;
	return glm::vec3( head.x, head.y, head.z );
}